package cn.practice.chat.export.demo;

import cn.practice.chat.export.utils.CellStyleStrategy;
import cn.practice.chat.export.utils.ColumnMergeUtils;
import cn.practice.chat.export.utils.RowMergeUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName 根据模板导出
 * @Description:
 * @Author A-TAO
 * @Date 2022/11/7
 * @Version V1.0
 **/
@Slf4j
@Controller
@RequestMapping
public class TempExportController {

    static List<TestDetail> list = new ArrayList<>();

    static {


        TestDetail detail1= new TestDetail();
        detail1.setSno("1001");
        detail1.setGroupName("分组1");
        detail1.setChara("外观");
        detail1.setRemark("不合格");
        detail1.setResult("NG");
        detail1.setScore("60");

        TestDetail detail2= new TestDetail();
        detail2.setSno("1001");
        detail2.setGroupName("分组1");
        detail2.setChara("目测");
        detail2.setRemark("合格");
        detail2.setResult("OK");
        detail2.setScore("90");


        TestDetail detail14= new TestDetail();
        detail14.setSno("1001");
        detail14.setGroupName("分组2");
        detail14.setChara("外观");
        detail14.setRemark("不合格");
        detail14.setResult("NG");
        detail14.setScore("60");
        TestDetail detail3= new TestDetail();
        detail3.setSno("1002");
        detail3.setGroupName("分组2");
        detail3.setChara("目测");
        detail3.setRemark("合格");
        detail3.setResult("OK");
        detail3.setScore("90");


        list.add(detail1);
        list.add(detail2);
        list.add(detail14);
        list.add(detail3);



    }

    @GetMapping("/download")
    public void workRecordExcelExport( HttpServletResponse response) {

        try{
            exportExcel(response);
        }catch(Exception e){
            log.error("导出异常");
            e.printStackTrace();
        }
    }


    public CellStyleStrategy horizontalCellStyleStrategyBuilder() {
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 10);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        return new CellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


    public void exportExcel(HttpServletResponse response) throws IOException {

        ColumnMergeUtils columnMergePrevColUtils = new ColumnMergeUtils();
        //设置第几列开始合并
        int[] mergeColumnIndex = {0, 1};
        //设置第几行开始合并
        int mergeRowIndex = 3;

        RowMergeUtils rowMergeStrategyUtils = new RowMergeUtils(mergeRowIndex, mergeColumnIndex);

        //模板位置
        InputStream template = new PathMatchingResourcePatternResolver().getResource("templates/test.xlsx").getInputStream();

        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        response.setHeader("Content-Disposition",
                "attachment;filename=" + java.net.URLEncoder.encode("测试导出.xlsx", "UTF-8"));
        //ExcelWriter该对象用于通过POI将值写入Excel
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(template)
                //设置样式
                .registerWriteHandler(horizontalCellStyleStrategyBuilder())
                //设置行合并单元格
                .registerWriteHandler(rowMergeStrategyUtils)
                //设置列合并单元格
                //.registerWriteHandler(excelFillCellMergePrevColUtils)
                .build();
        //构建excel的sheet
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        //Map<String, String> fileData = new HashMap<>();
//        fileData.put("name", name);
//        fileData.put("company", company);
//        excelWriter.fill(fileData, writeSheet);
        excelWriter.fill(list, writeSheet);
        excelWriter.finish();

    }



}
